#! python
# -*- coding: utf-8 -*-
from objects_on_map import Pixel, get_cross_pixels
from noise import get_around_pixels


def generate_portals_on_map(altitude_noise):
    y = 0
    pixel = Pixel()

    for y_height in altitude_noise:
        x = 0

        for x_pixel in y_height:
            # Dirt
            if x_pixel == 0:
                rock_sides = {"up": False, "down": False, "left": False, "right": False}
                dirt_sides = {"up": False, "down": False, "left": False, "right": False}

                rock_counter = 0

                pixel.x = x
                pixel.y = y

                around_pixels_near_dirt = get_around_pixels(pixel, 1)
                for one_pixel in around_pixels_near_dirt:
                    if altitude_noise[one_pixel[1]][one_pixel[0]] == 1:
                        rock_counter += 1

                if rock_counter > 1:

                    offset = 15
                    crosspixels_dict = get_cross_pixels(pixel, offset, altitude_noise)

                    for side in crosspixels_dict.iterkeys():
                        rock_counter = 0
                        dirt_counter = 0

                        side_pixels = crosspixels_dict[side]

                        for one_pixel in side_pixels:
                            value_in_altitude_list = altitude_noise[one_pixel[1]][one_pixel[0]]

                            if value_in_altitude_list in (1, 2, 3):
                                rock_counter += 1
                            elif value_in_altitude_list == 0:
                                dirt_counter += 1

                        if rock_counter == offset:
                            rock_sides[side] = True
                            #altitude_noise[y][x] = 23
                        if dirt_counter == offset:
                            #altitude_noise[y][x] = 24
                            dirt_sides[side] = True

                    rock_counter = 0

                    for side in rock_sides.iterkeys():
                        if rock_sides[side]:
                            rock_counter += 1

                    dirt_counter = 0

                    for side in dirt_sides.iterkeys():
                        if dirt_sides[side]:
                            dirt_counter += 1

                    if rock_counter == 2 and dirt_counter == 1:
                        offset = 2
                        rock_counter = 0
                        dirt_counter = 0

                        around_pixels = get_around_pixels(pixel, offset)

                        for one_pixel in around_pixels:
                            value_in_altitude_list = altitude_noise[one_pixel[1]][one_pixel[0]]

                            if value_in_altitude_list in (1, 2, 3):
                                rock_counter += 1
                            elif value_in_altitude_list == 0:
                                dirt_counter += 1

                        if rock_counter > dirt_counter:
                            offset = 40
                            portal_counter = 0
                            dirt_counter = 0

                            around_pixels = get_around_pixels(pixel, offset)

                            for one_pixel in around_pixels:
                                value_in_altitude_list = altitude_noise[one_pixel[1]][one_pixel[0]]

                                if value_in_altitude_list == 0:
                                    dirt_counter += 1
                                if value_in_altitude_list == 40:
                                    portal_counter += 1

                            if portal_counter == 0 and dirt_counter < 1200:
                                altitude_noise[y][x] = 40

            x += 1
        y += 1

    return altitude_noise